package com.lingz.manager.modules.camera.controller;

import com.lingz.manager.base.DevplatBaseController;
import com.lingz.manager.common.utils.ResultUtil;
import com.lingz.manager.common.vo.PageVo;
import com.lingz.manager.common.vo.Result;
import com.lingz.manager.common.vo.SearchVo;
import com.lingz.manager.modules.camera.entity.Camera;
import com.lingz.manager.modules.camera.service.CameraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


/**
 * @author fanyoucai
 */
@Slf4j
@RestController
@Api(description = "摄像头管理接口")
@RequestMapping("/EnjoyGIS/camera")
@Transactional
public class CameraController extends DevplatBaseController<Camera, String>{

    @Value("${mine.serviceUrl}")
    private String serviceUrl;

    @Autowired
    private CameraService cameraService;

    @Override
    public CameraService getService() {
        return cameraService;
    }

    @RequestMapping(value = "/findByCondition",method = RequestMethod.POST)
    @ApiOperation(value = "多条件分页获取矿山摄像头列表")
    public Result<Page<Camera>> findByCondition(@ModelAttribute Camera camera,
                                    @ModelAttribute SearchVo searchVo,
                                    @ModelAttribute PageVo pageVo){
        int pageNumber = pageVo.getPageNumber();
        int pageSize = pageVo.getPageSize();
        Page<Camera> cameras = cameraService.
                findByCondition(camera, searchVo, PageRequest.of(pageNumber - 1, pageSize));
        return new ResultUtil<Page<Camera>>().setData(cameras);
    }


    @RequestMapping(value = "/requestCameraListToDB",method = RequestMethod.GET)
    @ApiOperation(value = "请求服务数据保存到本地数据库")
    public Result<Object> requestCameraListToDB(){
         cameraService.requestCameraListToDB(serviceUrl);
         return new ResultUtil<Object>().setData("同步成功");
    }



    @RequestMapping(value = "/updateCamera",method = RequestMethod.POST)
    @ApiOperation(value = "修改坐标")
    public Result<Object> update(@RequestParam String id,
                                 @RequestParam String altitude,
                                 @RequestParam String latitude,
                                 @RequestParam String longitude,
                                 @RequestParam String type){
        Camera camera = cameraService.get(id);
        camera.setAltitude(altitude);
        camera.setLatitude(latitude);
        camera.setLongitude(longitude);
        camera.setType(type);
        camera.setPositionStatus("1");
        Camera update = cameraService.update(camera);
        return new ResultUtil<Object>().setData(update);
    }


}
